package civaid;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class Parameters {

	private Map<String, String[]> values = new HashMap<String, String[]>();
	private HttpServletRequest request;

	public Parameters(HttpServletRequest r) {
		request = r;
		values = r.getParameterMap();

	}

	public String getParameter(String key, String dfl) {
		return getParameters(key, new String[] { dfl })[0];
	}

	public String[] getParameters(String key, String[] dfl) {
		String[] v = values.get(key);
		if (v == null || v.length == 0 || v[0] == null)
			return dfl;
		return v;
	}

	public Enum getEnumParameter(String key, Class<? extends Enum> dfl) {

		String v = getParameter(key, dfl.toString());
		return Enum.valueOf(dfl, v);

	}

	public Object getJSON(Type type) {
		try {
			return JSON.fromJson(request.getReader(), type);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

}
